"use strict";
/* Controllers */
app.controller("kpiDetailAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'kpiDetailAddService', function ($scope, $location, $rootScope, $routeParams, kpiDetailAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.groupId = $routeParams.groupId;
    vm.p.kpiId = $routeParams.kpiId;
    vm.p.id = $routeParams.id;


    var errorFn = function (result) {
        alert(result);
    };

    $scope.addKpiDetail = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        kpiDetailAddService.addKpiDetail(params, procFn, errorFn);
    };

    $scope.getKpiDetail = function () {
        var vm = $scope.vm;
        var params = {
            kpiId: vm.p.kpiId,
            id: vm.p.id
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //vm.p = JSON.stringify(response.data);
                vm.p = response.data;
            } else {
                alert(response.msg);
            }
        };
        kpiDetailAddService.getKpiDetail(params, procFn, errorFn);
    };
    if(vm.p.id!=0) {
        console.log(vm.p.kpiId + ',' + vm.p.id);
        $scope.getKpiDetail();
    }
}]);


/* Services */
app.service('kpiDetailAddService', ['ajaxService', function (ajaxService) {

    this.addKpiDetail = function (metricKpiDetails, successFunction, errorFunction) {
        ajaxService.AjaxPost(metricKpiDetails, "/star/api/basic-service/v1/kpi_detail/setKpiDetail.do", successFunction, errorFunction);
    };

    this.getKpiDetail = function (metricKpiDetails, successFunction, errorFunction) {
        ajaxService.AjaxPost(metricKpiDetails, "/star/api/basic-service/v1/kpi_detail/getKpiDetail.do", successFunction, errorFunction);
    };

}]);

